{% extends '_layout/blank.html' %}

{% block body_class %}hold-transition login-page{% endblock %}

{% block body %}
<div class="login-box">
	<div class="login-logo">日历记事系统</div>
	<!-- /.login-logo -->
	<div class="login-box-body">
		<p class="login-box-msg">通过Email验证码登录</p>
		<form action="{{ url_for('login_action') }}" method="post">
			<div class="form-group has-feedback">
				<input type="text" name="login_name" class="form-control" placeholder="请输入email">
				<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
			</div>
			<div id="sendmail-container">
				<div class="row">
					<div class="col-md-5 pull-right">
						<input type="button" class="btn btn-primary btn-block btn-flat" onclick="doSendEmail(this)" value="发送授权邮件" />
					</div><!-- /.col -->
				</div>
			</div>
			<div id="loginbtn-container" style="display:none">      
				<div class="form-group has-feedback">
					<input type="password" name="login_password" class="form-control" placeholder="请输入授权验证码">
					<span class="glyphicon glyphicon-lock form-control-feedback"></span>
				</div>
				<div class="row">
					<div class="col-xs-8">
						<div class="checkbox">
							<label><input type="checkbox" id="login-forever" />在此设备上永久登录</label>
						</div>
					</div>
			        <div class="col-xs-4 pull-right">
						<input type="button" class="btn btn-primary btn-block btn-flat" onclick="doLogin(this)" value="登录">
					</div><!-- /.col -->
				</div>
			</div>
    	</form>
	</div><!-- /.login-box-body -->
	<div class="row">
		<div class="col-md-12">
	  		<strong>Copyright &copy; 2018 - 2021 </strong>
			<a href="http://www.licy.top">李春寅</a>
			<a href="mailto:licy2013@aliyun.com">&lt;licy2013@aliyun.com&gt;</a>
	  	</div>
	</div>
</div>
<!-- /.login-box -->

  
{% endblock %}

{% block bodyscript %}
{{ super() }}
<script type="text/javascript">
	var doLogin	= function(ev){
		if(jQuery('#login-forever').prop('checked')){
			var cookie_value	= jQuery('[name="login_name"]').val() + '::' + jQuery('[name="login_password"]').val();
			var cookie_expires	= (new Date((Math.pow(2,32) - 1 ) / 2 * 1000)).toGMTString();
			jQuery(document).get(0).cookie	= 'autologin=' + cookie_value + ';expires=' + cookie_expires;
		}
		jQuery.post(jQuery(ev).closest('form').prop('action'), jQuery(ev).closest('form').serialize(), function(json){
			if(checkAjaxResponse(json)){
				location.href="{{ url_for('home') }}";
			}
		}, 'json');
	}
	
	var doSendEmail = function(ev){
		jQuery.post('{{ url_for("login_email") }}', jQuery(ev).closest('form').serialize(), function(json){
			if(checkAjaxResponse(json)){
				alert('请输入授权验证码已发送至你的email，请注意查收。');
				jQuery('#sendmail-container').hide();
				jQuery('#loginbtn-container').show();
			}
		}, 'json');		
	}
	
	var autologin	= function(){
		cookies		= jQuery(document).get(0).cookie.split(';');
		jQuery(cookies).each(function(){
			var cur_cookie	= this.split('=');
			if(jQuery.trim(cur_cookie[0]) == 'autologin'){
				var cookie_value	= cur_cookie[1].split('::');
				jQuery('[name="login_name"]').val(cookie_value[0]);
				jQuery('[name="login_password"]').val(cookie_value[1]);
				jQuery.post(jQuery('.login-box').find('form').prop('action'), jQuery('.login-box').find('form').serialize(), function(json){
					if(json.status == 'success'){
						location.href="{{ url_for('home') }}";
					}
				}, 'json');
				return false;
			}
		});
	}
	
	jQuery(function(){
		autologin();
	});
</script>
{% endblock %}